package com.wyh.lotterysystem.common.aspect;

import com.wyh.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.wyh.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.wyh.lotterysystem.common.exception.ControllerException;
import com.wyh.lotterysystem.common.exception.ServiceException;
import com.wyh.lotterysystem.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
@ResponseBody
public class ExceptionAspect {

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<String> controllerException(ControllerException e){
        //打印日志
        log.error("serviceException:",e);

        //返回
        return CommonResult.fail(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMsg());
    }

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<String> serviceException(ServiceException e){
        //打印日志
        log.error("serviceException:",e);

        //返回
        return CommonResult.fail(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMsg());
    }


    @ExceptionHandler(value = Exception.class)
    public CommonResult<String> exception(Exception e){
        //打印日志
        log.error("服务异常：",e);

        //返回
        return CommonResult.fail(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),e.getMessage());
    }
}
